package module;




import java.util.List;
import java.util.concurrent.TimeUnit;

import org.jdom2.Element;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.BeforeTest;

import automation.readxmlFile.readFileConfig;
import automation.readxmlFile.testProfile;
import sms.automation.common.Constant;
import until.Reporter;
import until.StopWatch;
import until.XMLParser;




public class ExtendTestSuite { 
	public Reporter reporter;
	public testProfile testprofile;
	public ThreadLocal<WebDriver> tdriver = new ThreadLocal<WebDriver>();
	
	public String testuserrole;
	public  XMLParser testprofileSaadmin;
	public  XMLParser testprofileAdmin;
	public  XMLParser testprofileTherapist;
	public  XMLParser testprofilePatient;
	public  XMLParser newfuntion;
	public XMLParser testconfig;
	public enum TestResult
	{
		
	Pass,Fail,Warning,Note	
		
	} 
	private int timetowaitfindElementmin=2;
	private int timetowaitfindelemantmax=14;
	
	
	public void initDriver (){
		WebDriver driver;
	if("Chrome".equals(Constant.driverPlatform)){
		 System.setProperty("webdriver.chrome.driver",
				Constant.CHROMEDRIVER_PATH);
		driver=new ChromeDriver();
		tdriver.set(driver);
		 
	}	
else {
 
if("Safari".equals(Constant.driverPlatform)){
	driver=new SafariDriver();
	
}

 
}
	
	//Read test config
	 
	testprofileSaadmin=readFileConfig.testprofileSaadmin;
	testprofileAdmin=readFileConfig.testprofileAdmin;
	testprofileTherapist=readFileConfig.testprofileTherapist;
	testprofilePatient=readFileConfig.testprofilePatient;
	testconfig=readFileConfig.testconfig;

    newfuntion=readFileConfig.newfuntion;
    
    
	}
	
	public WebDriver getDriver(){
		
		return tdriver.get();
	}
	
	

	
	@BeforeTest
	public void testInit(){
		
		readFileConfig.InitTest();
	} 
public boolean Pasource(String text){
	boolean iscontain=true;
	
   getDriver().getPageSource();
	System.out.println( getDriver().getPageSource());
	return iscontain;
	
}
/*public boolean clickOn(String elementString) {

	// Find an element
	WebElement targetElement =findElement(elementString);

	return clickOn(targetElement);
}*/
//Click on element
	public boolean clickOn(WebElement targetElement) {

		boolean isSuccess = false;

		// Click on element
		StopWatch timer = new StopWatch();
		timer.start();

		//in view port 
		Coordinates coordinate = ((Locatable)targetElement).getCoordinates();
		coordinate.inViewPort();
		

		targetElement.click();

	/*	// Get time that take to loading target url
		long loadTime = timer.getElapsedTimeSecs();

		// Warning because of long loading time
		if (loadTime > 10) {
			reporter.addReport(
					getSuiteName(),
					getCurrentCase(),
					"Long loading time:  "
							+ CommonServices.getElementPath(targetElement)
							+ " -> Loadtime: " + loadTime + "s",
					TestResult.WARNING);
		}

		// Is there error page after click?
		if (checkErrorPage() == true) {
			reporter.addReport(
					getSuiteName(),
					getCurrentCase(),
					"Click on: " + CommonServices.getElementPath(targetElement)
							+ "  => Error!"
							+ URLServices.hrefLink(takeScreenShot(), " Screenshot"),
					TestResult.FAIL);
		} else {
			isSuccess = true;
		}
*/
		return isSuccess;
	}
	
	// Find an element
	public WebElement findElement(String element) {
		return findElement(element, -1);
	}
	
	// Find an element
	public WebElement findElement(String element, int elementIndex) {

		// Is this element must be read from profile?
		if (element.contains("|")) {

			if (elementIndex == -1) {
				element = testprofile.readNode(element);
			} else {
				element =testprofile.readElement(element, elementIndex);
			}
		}

		WebElement result = null;

		// XPath
		if (element.contains("//")) {
			result = getDriver().findElement(By.xpath(element));
		}

		// CSS selector
		if (element.contains("html ")) {
			result = getDriver().findElement(By.cssSelector(element));
		} else if (element.contains("css::")) {
			String[] arrElement = element.split("::");
			result = getDriver().findElement(By.cssSelector(arrElement[1]));

		}

		// ID
		if (element.contains("id::")) {
			String[] arrElement = element.split("::");
			result = getDriver().findElement(By.id(arrElement[1]));
		}

		// Class
		if (element.contains("class::")) {
			String[] arrElement = element.split("::");
			result = getDriver().findElement(By.className(arrElement[1]));
		}

		

		return result;
	}
     //Find element of systemadmin
	
	public WebElement findElementSystemAdmin(String element) {
		return findElementSystemAdmin(element, -1);
	}
	
	// Find an element of SystemAdmin
		public WebElement findElementSystemAdmin(String element, int elementIndex) {

			// Is this element must be read from profile?
			if (element.contains("|")) {

				if (elementIndex == -1) {
					element = testprofileSaadmin.readRootNode(element);
				} else {
					element =testprofileSaadmin.readNodeByname(element);
					element = element.replace("$", Integer.toString(elementIndex));
				}
			}

			WebElement result = null;

			// XPath
			if (element.contains("//")) {
				result = getDriver().findElement(By.xpath(element));
			}

			// CSS selector
			if (element.contains("html ")) {
				result = getDriver().findElement(By.cssSelector(element));
			} else if (element.contains("css::")) {
				String[] arrElement = element.split("::");
				result = getDriver().findElement(By.cssSelector(arrElement[1]));

			}

			// ID
			if (element.contains("id::")) {
				String[] arrElement = element.split("::");
				result = getDriver().findElement(By.id(arrElement[1]));
			}

			// Class
			if (element.contains("class::")) {
				String[] arrElement = element.split("::");
				result = getDriver().findElement(By.className(arrElement[1]));
			}

			

			return result;
		}
	// Find all elements
	public List<WebElement> findElements(String element) {
		return findElements(element, -1);
	}
	
	public List<WebElement> findElements(String element, int index) {

		// Is this element must be read from profile?
		if (element.contains("|")) {
			if (index == -1) {
				element = testprofile.readElement(element);
			} else {
				element =testprofile.readElement(element, index);
			}
		}

		List<WebElement> results = null;

		// XPath
		if (element.contains("//")) {
			results = getDriver().findElements(By.xpath(element));
		}

		// CSS selector
		if (element.contains("html ") && !element.contains("css::")) {
				results = getDriver().findElements(By.cssSelector(element));
			
		} else if (element.contains("css::")) {
			String[] arrElement = element.split("::");
			results = getDriver().findElements(By.cssSelector(arrElement[1]));
		}

		// ID
		if (element.contains("id::")) {
			String[] arrElement = element.split("::");
			results = getDriver().findElements(By.id(arrElement[1]));
		}

		// Class
		if (element.contains("class::")) {
			String[] arrElement = element.split("::");
			results = getDriver().findElements(By.className(arrElement[1]));
		}

		return results;
	}



}
